################################################################################
#
#                       美国民粹主义外交政策的民意基础                       
#
#                            付 舒    2025年10月     
#
################################################################################


#####（二）因子分析测量民粹指数  ######

library(tidyverse)
library(lavaan)
library(psy)

# calculate populism score by factor analyses

m3a20 <- 'political_fact =~ gov_trust + polit_num_corrupt + wide_corruption +
                          polit_no_care + polit_trust + polit_problem +
                          polit_interest_rich + polit_no_say'
polit_fac20 <- cfa(m3a20, data = anes2020, std.lv = T)
fitmeasures(polit_fac20, c('cfi', 'rmsea', 'rmsea.ci.upper', 'bic'))
summary(polit_fac20, fit.measures = T, standardized = T)
polit_predicted20 <- as.data.frame(predict(polit_fac20))

anes_political20 <- anes2020 %>% 
  dplyr::select(gov_trust, polit_num_corrupt, wide_corruption, 
                polit_no_care, polit_trust, polit_problem, 
                polit_no_say, polit_interest_rich)
cronbach(anes_political20)


m2a20 <- 'economic_fact =~ immig_take_job + immig_good_econ + finance_worry +
                         trade_increase + free_trade7'
econ_fac20 <- cfa(m2a20, data = anes2020, std.lv = T)
fitmeasures(econ_fac20, c('cfi', 'rmsea', 'rmsea.ci.upper', 'bic'))
summary(econ_fac20, fit.measures = T, standardized = T)
econ_predicted20 <- as.data.frame(predict(econ_fac20))

anes_economic20 <- anes2020 %>% 
  dplyr::select(immig_take_job, immig_good_econ, finance_worry, 
                trade_increase, free_trade7)
cronbach(anes_economic20)


m1a20 <- 'cultural_fact =~ immig_policy + syrian_refug + 
                         immig_restrict + america_customs + muslimft + 
                         illeg_immig_ft + govt_help_black + mexico_wall'
cult_fac20 <- cfa(m1a20, data = anes2020, std.lv = T)
fitmeasures(cult_fac20, c('cfi', 'rmsea', 'rmsea.ci.upper', 'bic'))
summary(cult_fac20, fit.measures = T, standardized = T)
cult_predicted20 <- as.data.frame(predict(cult_fac20))

anes_cultural20 <- anes2020 %>% 
  dplyr::select(immig_policy, syrian_refug,
                immig_restrict, america_customs, muslimft,
                illeg_immig_ft, govt_help_black, mexico_wall)
cronbach(anes_cultural20)


anes2020_popu <- 
  cbind(anes2020, cult_predicted20, econ_predicted20, polit_predicted20) %>%
  mutate(popu_cult = (cultural_fact - min(cultural_fact)) / 
           (max(cultural_fact) - min(cultural_fact)),
         popu_econ = (economic_fact - min(economic_fact)) / 
           (max(economic_fact) - min(economic_fact)),
         popu_poli = (political_fact - min(political_fact)) / 
           (max(political_fact) - min(political_fact))) %>%
  drop_na(cv_vote2020trump, cv_ideology, cv_party, 
          cv_religion_catho, cv_religion_chris, 
          cv_houseowner, cv_female, cv_age,cv_marital_mar, 
          cv_race_white, cv_race_black, cv_race_latin, 
          cv_education, cv_income)



